package com.village.zpark.controller;

import com.github.pagehelper.PageInfo;
import com.village.zpark.common.ResultMapUtil;
import com.village.zpark.pojo.Water;
import com.village.zpark.service.WaterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/water")
public class WaterController {
    @Autowired
    private WaterService waterService;


    @RequestMapping
    public String Water(){
        return "/water";
    }

    @RequestMapping(value = "/waterPage")
    public String WaterPage(){
        return "/waterPage";
    }

    @RequestMapping(value = "/waterQueryById")
    public Object WaterQueryById(@RequestParam(name = "id",required = true) Integer id, Model model){
        Water water = waterService.findById(id);
        model.addAttribute("obj",water);
        return "/waterPage";
    }

    @RequestMapping(value = "/waterList")
    @ResponseBody
    public Object WaterList(){
        List<Water> waterList = waterService.findAll();
        return ResultMapUtil.getHashMapList(waterList);
    }


    @RequestMapping(value = "/addwater")
    @ResponseBody
    public Object addWater(Water water){
        try {
            int i = waterService.addWater(water);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }


    @RequestMapping(value = "/updatewater")
    @ResponseBody
    public Object updateWater(Water water){
        try {
            int i = waterService.updateWater(water);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    @RequestMapping(value = "/deletewater")
    @ResponseBody
    public Object deleteWater(Integer id){
        try {
            int i = waterService.deleteWaterById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }


    @RequestMapping(value = "/waterQueryPage")
    @ResponseBody
    public Map<String, Object> WaterQueryPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String param) {
        PageInfo<Water> waterPageInfo = waterService.findPage(page, limit, param);
        Map<String,Object> map=new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询数据成功");
        map.put("count", waterPageInfo.getTotal());
        map.put("data", waterPageInfo.getList());
        return map;
    }
}
